package com.fantsey.test.juejin;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 * <a href="https://www.marscode.cn/practice/plp543172djj1j?problem_id=7424418560666877996">题目原链接</a>
 * 1.小E可以击败血量和攻击力都小于她当前属性的怪物
 * 2.对于每只怪物，小E可以选择与它战斗或者跳过这只怪物
 * 3.为了保持战斗节奏，要求击败的怪物序列中，后一个怪物的血量和攻击力都必须严格大于前一个怪物
 * **输入 **
 * n：怪物的数量
 * H：小E的血量
 * A：小E的攻击力
 * h[i]：第i个怪物的血量
 * a[i]：第i个怪物的攻击力
 * **输出 **
 * 返回小E最多能击败的怪物数量
 * 样例1：
 * 输入：n = 3, H = 4, A = 5, h = [1, 2, 3], a = [3, 2, 1]                  ------[4,5]----[1,3] [2,2] [3,1]
 * 输出：1
 * 样例2：
 * 输入：n = 5, H = 10, A = 10, h = [6, 9, 12, 4, 7], a = [8, 9, 10, 2, 5]  ------[10,10]----[6,8] [9,9] [12,10] [4,2] [7,5]
 * 输出：2
 * 样例3：
 * 输入：n = 4, H = 20, A = 25, h = [10, 15, 18, 22], a = [12, 18, 20, 26]  ------[20,25]----[10,12] [15,18] [18,20] [22,26]
 * 输出：3
 * 样例4：
 * 输入：n = 4, H = 20, A = 25, h = [22, 18, 15, 10], a = [26, 20, 18, 12]  ------[20,25]----[22,26] [18,20] [15,18] [10,12]
 * 输出： 1
 *
 * @author fx
 * @date 2024/12/27
 * @since 1.0
 */
public class Question6 {


    /**
     * 思路： 先将怪物的血量和攻击力组合分开。
     * 1. 从第一只开始遍历判断
     * 1） 是否可以打过，打不过则跳过
     * 2） 当前的怪物，血量和攻击力是否比前一只要高，
     *  2.1） 如果高，则判断是否可以打过，如果可以打过，则继续打
     *  2.2） 如果低，则跳过
     * 2. 遍历到最后一只，记录能打的数量
     * 3. 从下一个只开始遍历，继续执行上述逻辑（递归）
     * 4. 最后比较几次的结果，返回最大的数量即为结果
     */
    public static int solution(int n, int H, int A, int[] h, int[] a) {
        // PLEASE DO NOT MODIFY THE FUNCTION SIGNATURE
        // write code here

        List<Integer> monsterHp = convertToList(h);
        List<Integer> monsterAttack = convertToList(a);

        List<Integer> beatCountList = new ArrayList<>();

        // 第1种方法：
       /* int lastMonsterHp = 0;
        int lastMonsterAttack = 0;
        for (int i=0; i<n; i++) {
            List<Integer> monsterHpItem = monsterHp.subList(i, n);
            List<Integer> monsterAttachItem = monsterAttack.subList(i, n);
            if (i != 0) {
                lastMonsterHp = monsterHp.get(i-1);
                lastMonsterAttack = monsterAttack.get(i-1);
            }
            int beatCountItem = getMaxMonsterBeatCount(n, H, A, monsterHpItem, monsterAttachItem, lastMonsterHp,
                    lastMonsterAttack, i);
            beatCountList.add(beatCountItem);
        }*/

        // 第2种方法：
        getMaxMonsterBeatCountRecursion(n, H, A, monsterHp, monsterAttack, 0, beatCountList);
        if (beatCountList.isEmpty()) {
            return 0;
        }
        System.out.println(beatCountList);
        return beatCountList.stream().max(Integer::compare).get();
    }

    public static int getMaxMonsterBeatCount(int monsterCount, int userHp, int userAttack, List<Integer> monsterHp,
                                             List<Integer> monsterAttack, int lastMonsterHp, int lastMonsterAttack,
                                             int currentMonsterGroupIndex) {
        int beatMonsterCount = 0;
        int remainMonsterGroupCount = monsterCount - currentMonsterGroupIndex;
        for (int i = 0; i < remainMonsterGroupCount; i++) {
            int currentMonsterHp = monsterHp.get(i);
            int currentMonsterAttack = monsterAttack.get(i);
            if (lastMonsterHp != 0 && lastMonsterAttack !=0 ) {
                if (currentMonsterHp < lastMonsterHp || currentMonsterAttack < lastMonsterAttack) {
                    continue;
                }
            }
            if (currentMonsterHp > userHp  || currentMonsterAttack > userAttack) {
                continue;
            }

            beatMonsterCount++;
            lastMonsterHp = currentMonsterHp;
            lastMonsterAttack = currentMonsterAttack;
        }
        return beatMonsterCount;
    }


    public static List<Integer> getMaxMonsterBeatCountRecursion(int monsterCount, int userHp, int userAttack, List<Integer> monsterHp,
                                             List<Integer> monsterAttack,int currentMonsterGroupIndex, List<Integer> resultList) {

        int beatMonsterCount = 0;
        int remainMonsterGroupCount = monsterCount - currentMonsterGroupIndex;
        int lastMonsterHp = 0;
        int lastMonsterAttack = 0;
        for (int i = 0; i < remainMonsterGroupCount; i++) {
            int currentMonsterHp = monsterHp.get(i);
            int currentMonsterAttack = monsterAttack.get(i);
            if (i!=0) {
                if (currentMonsterHp <= lastMonsterHp || currentMonsterAttack <= lastMonsterAttack) {
                    continue;
                }
            }
            if (currentMonsterHp >= userHp  || currentMonsterAttack >= userAttack) {
                continue;
            }

            beatMonsterCount++;
            lastMonsterHp = currentMonsterHp;
            lastMonsterAttack = currentMonsterAttack;
        }

        if (currentMonsterGroupIndex == monsterCount) {
            return resultList;
        }

        currentMonsterGroupIndex++;
        List<Integer> nextMonsterHp = monsterHp.subList(1, monsterHp.size());
        List<Integer> nextMonsterAttack = monsterAttack.subList(1, monsterAttack.size());
        resultList.add(beatMonsterCount);

        getMaxMonsterBeatCountRecursion(monsterCount, userHp, userAttack, nextMonsterHp,
                nextMonsterAttack, currentMonsterGroupIndex, resultList);
        return resultList;
    }

    private static List<Integer> convertToList(int[] data) {
        List<Integer> list = new ArrayList<>();
        for (int datum : data) {
            list.add(datum);
        }
        return list;
    }

    public static void main(String[] args) {
        /*System.out.println(solution(3, 4, 5, new int[]{1, 2, 3}, new int[]{3, 2, 1}) == 1);
        System.out.println(solution(5, 10, 10, new int[]{6, 9, 12, 4, 7}, new int[]{8, 9, 10, 2, 5}) == 2);
        System.out.println(solution(4, 20, 25, new int[]{10, 15, 18, 22}, new int[]{12, 18, 20, 26}) == 3);

        System.out.println(solution(14, 15, 6, new int[]{15,4,10,15,2,6,16,10,17,17,6,9,14,3},
                new int[]{10,8,6,7,7,1,17,3,3,4,1,2,4,13}));

        System.out.println(solution(13, 8, 7, new int[]{8,4,1,5,11,8,8,8,3,2,13,1,16},
                new int[]{16,7,6,7,8,1,9,14,14,3,12,14,11}));*/

        System.out.println(solution(13, 17, 14, new int[]{2,3,12,13,7,8,5,15,1,12,17,14,4},
                new int[]{13,13,8,15,2,5,7,8,13,7,6,8,1}));
    }
}
